package com.polaris.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.polaris.common.utils.RespBean;
import com.polaris.server.dto.LogLoginParam;
import com.polaris.server.pojo.LogLogin;
import com.polaris.server.mapper.LogLoginMapper;
import com.polaris.server.service.ILogLoginService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author polaris
 * @since 2021-06-08
 */
@Service
public class LogLoginServiceImpl extends ServiceImpl<LogLoginMapper, LogLogin> implements ILogLoginService {

    private final LogLoginMapper logLoginMapper;

    public LogLoginServiceImpl(LogLoginMapper logLoginMapper) {
        this.logLoginMapper = logLoginMapper;
    }

    /**
     * 获取所有的登录日志
     *
     * @return
     */
    @Override
    public List<LogLogin> queryAll() {
        return logLoginMapper.selectList(new QueryWrapper<>());
    }

    /**
     * 通过检索条件获取登录日志
     *
     * @param param
     * @param page
     * @return
     */
    @Override
    public RespBean selectByParam(LogLoginParam param, Page<LogLogin> page) {
        HashMap<String, Object> resultMap = new HashMap<>();
        QueryWrapper<LogLogin> queryWrapper = new QueryWrapper<>();
        Map<String, Object> beanToMap = BeanUtil.beanToMap(param,true,false);
        queryWrapper.allEq(beanToMap, false);
        Page<LogLogin> selectPage = logLoginMapper.selectPage(page, queryWrapper);
        resultMap.put("total", selectPage.getTotal());
        resultMap.put("data", selectPage.getRecords());
        return RespBean.success("获取登陆日志成功",resultMap);
    }
}
